package com.xiaoyu.lanling.feature.visitor.data

import `in`.srain.cube.request.FailData
import `in`.srain.cube.request.JsonData
import `in`.srain.cube.request.RequestHandler
import `in`.srain.cube.views.list.PagedListDataModelWithRequestTag
import com.xiaoyu.base.net.RequestCreator
import com.xiaoyu.lanling.common.config.HttpConfig
import com.xiaoyu.lanling.event.visitor.VisitorListEvent
import com.xiaoyu.lanling.feature.visitor.model.VisitorItem

class VisitorListViewData() : PagedListDataModelWithRequestTag<VisitorItem, Any>() {

    val tag = Any()
    override fun doQueryData() {

        RequestCreator.create(VisitorListEvent::class.java)
                .setRequestUrl(HttpConfig.LANLING_GET_MY_VISITOR)
                .appendQueryDataTo(listPageInfo)
                .setRequestHandler(object : RequestHandler<VisitorListEvent, JsonData> {
                    override fun onRequestFail(failData: FailData?) {

                    }

                    override fun processOriginData(originData: JsonData): VisitorListEvent {

                        val rawData = originData.optJson("data")

                        val list = mutableListOf<VisitorItem>()

                        val rawList = rawData.optJson("list").toList()

                        for ((index, item) in rawList.withIndex()) {
                            list.add(VisitorItem(item))
                        }

                        updateResult(rawData, list)

                        return VisitorListEvent(requestTag)
                    }

                    override fun onRequestFinish(event: VisitorListEvent) {

                        event.post()
                    }

                })
                .enqueue()
    }

    override fun getRequestTag(): Any {

        return tag
    }

}